<?php

use Biz\model\Album\AlbumListModel;
use Biz\model\Album\AlbumWikiListModel;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;

/**
 * wiki好物清单推荐语
 * Class AlbumRecommendationAction
 */
class AlbumRecommendationAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $page = $this->getRequest()->getQuery('page', 1);
        $wikiId = $this->getRequest()->getQuery('wiki_id', 0);
        $pageSize = 20;
        $count = 0;
        $list = [];
        if ($wikiId) {
            $albumWikiList = AlbumWikiListModel::selectByWhere([
                'wiki_id' => $wikiId,
                'status' => AlbumWikiListModel::STATUS_NORMAL,
                'content[!]' => '',
                'ORDER' => ['addtime' => 'DESC', 'id' => 'DESC']
            ], ['id', 'album_id', 'content', 'addtime', 'obj_id', 'obj_type']);
            $data = [];
            if ($albumWikiList) {
                $albumIds = array_column($albumWikiList, 'album_id');
                $albumWikiList = array_column($albumWikiList, null, 'album_id');
                $albumList = AlbumListModel::selectByWhere([
                    'album_id' => $albumIds,
                    'status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP,
                    'quality[!]' => AlbumListModel::QUALITY_BAD,
                ], ['album_id', 'uid', 'title']);
                $albumList = $albumList ? array_column($albumList, null, 'album_id') : [];
                foreach ($albumWikiList as $albumId => $albumWikiInfo) {
                    $albumInfo = $albumList[$albumId] ?? [];
                    if ($albumInfo) {
                        $uid = 0;
                        if ($albumWikiInfo['obj_type'] == BlockType::TYPE_PHOTO && $albumWikiInfo['obj_id']) {
                            $uid = IdParse::getUid($albumWikiInfo['obj_id']);
                        }
                        $uid = $uid ?: IdParse::getUid($albumId);
                        $data[$albumId] = [
                            'uid' => $uid,
                            'nick' => '',
                            'avatar' => '',
                            'emblem_adorn' => null,
                            'addtime' => strtotime($albumWikiInfo['addtime']),
                            'content' => $albumWikiInfo['content'],
                            'album_id' => $albumId,
                            'album_title' => $albumInfo['title'],
                            'statSign' => statSign::genStatSign(statSign::MODULE_GOODS_ALBUM_LIST, [
                                statSign::EXTEND_KEY_OBJ_ID => $albumId,
                                statSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_ALBUM,
                                statSign::EXTEND_KEY_RELATE_OBJ_ID => $wikiId,
                                statSign::EXTEND_KEY_RELATE_OBJ_TYPE => BlockType::TYPE_WIKI
                            ])
                        ];
                    }
                }
            }
            $count = count($data);
            $data = array_slice($data, ($page - 1) * $pageSize, $pageSize);
            $uids = [];
            foreach ($data as $row) {
                $uids[] = $row['uid'];
            }
            $uids = array_unique(array_filter($uids));
            $userList = $uids ? User::getUsers($uids) : [];
            foreach ($data as $albumId => $info) {
                $uid = $info['uid'];
                $userInfo = $userList[$uid] ?? [];
                $info['nick'] = $userInfo['nick'] ?? '';
                $info['avatar'] = $userInfo['avatar'] ?? '';
                $info['emblem_adorn'] = $userInfo['emblem_adorn'] ?? null;
                $list[] = $info;
            }
        }
        $isOver = ($page * $pageSize >= $count) ? 1 : 0;
        $isOver = empty($list) ? 1 : $isOver;
        $this->setSuccess(['list' => $list, 'is_over' => $isOver, 'count' => $count]);
    }

}